package com.hymjweb.demo.hymj.bill_code_manage.b_master.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.bill_code_manage.b_master.bean.BMasterBillCodePojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BMasterBillCodeMapper {



    class BMasterBillCodeProvider {

        public String fetch(String id) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_master_bill_code t");
                WHERE("1=1 and t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String findAll(Map map) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_master_bill_code t");
                WHERE("1=1");
            }}.toString();

            /**
             * keyAgentCompanyId: "383446380553256960"
             * keyStartGenerateDate: "2020-11-04"
             * keyEndGenerateDate: "2020-11-04"
             * keyGenerateDepartmentId: "385640538562428928"
             * keyGenerateUserId: "201910162259560401"
             * keyId: "123456"
             * keyUseStatusId: "241"
             * keyXtAirlinesId: "10002"
             */
            if (map != null) {
                String keyId = DataTypeUtil.toNotNullString(map.get("keyId"));
                Long keyUseStatusId = DataTypeUtil.toLong(map.get("keyUseStatusId"), -1L);
                String keyGenerateDepartmentId = DataTypeUtil.toNotNullString(map.get("keyGenerateDepartmentId"));
                String keyXtAirlinesId = DataTypeUtil.toNotNullString(map.get("keyXtAirlinesId"));
                String keyStartGenerateDate = DataTypeUtil.toNotNullString(map.get("keyStartGenerateDate"));
                String keyEndGenerateDate = DataTypeUtil.toNotNullString(map.get("keyEndGenerateDate"));
                String keyGenerateUserId = DataTypeUtil.toNotNullString(map.get("keyGenerateUserId"));
                String keyAgentCompanyId = DataTypeUtil.toNotNullString(map.get("keyAgentCompanyId"));

                // select * from test where date_format(create_time,'%Y-%m-%d') between '2019-03-05' and '2019-03-08'
                if (!DataTypeUtil.isEmptyStr(keyId)) {
                    sql += " and t.id like " + DataTypeUtil.quoteAllLikeStr(keyId);
                }

                if (keyUseStatusId != -1L) {
                    sql += " and t.use_status_id = " + keyUseStatusId;
                }
                if (!DataTypeUtil.isEmptyStr(keyGenerateDepartmentId)) {
                    sql += " and t.generate_department_id =" + DataTypeUtil.quoteStr(keyGenerateDepartmentId);
                }
                if (!DataTypeUtil.isEmptyStr(keyXtAirlinesId)) {
                    sql += " and t.airlines_id =" + DataTypeUtil.quoteStr(keyXtAirlinesId);
                }
                if (!DataTypeUtil.isEmptyStr(keyStartGenerateDate) && !DataTypeUtil.isEmptyStr(keyEndGenerateDate)) {
                    sql += " and (date_format(generate_date,'%Y-%m-%d') between " + DataTypeUtil.quoteStr(keyStartGenerateDate) + " and " + DataTypeUtil.quoteStr(keyEndGenerateDate) + ")";
                }
                if (!DataTypeUtil.isEmptyStr(keyGenerateUserId)) {
                    sql += " and t.generate_user_id =" + DataTypeUtil.quoteStr(keyGenerateUserId);
                }
                if (!DataTypeUtil.isEmptyStr(keyAgentCompanyId)) {
                    sql += " and t.agent_company_id =" + DataTypeUtil.quoteStr(keyAgentCompanyId);
                }
            }

            return sql;
        }


        public String update(BMasterBillCodePojo bMasterBillCodePojo) {
            String sql = new SQL() {{
                UPDATE("b_master_bill_code t");
                SET("t.airlines_id = #{airlinesId}");
                SET("t.use_status_id = #{useStatusId}");
                SET("t.generate_department_id = #{generateDepartmentId}");
                SET("t.generate_date = #{generateDate}");
                SET("t.generate_user_id = #{generateUserId}");
                SET("t.auth_use_department_id = #{authUseDepartmentId}");
                SET("t.use_department_id = #{useDepartmentId}");
                SET("t.use_user_id = #{useUserId}");
                SET("t.agent_company_id = #{agentCompanyId}");
                WHERE("t.id=#{id}");
            }}.toString();
            return sql;
        }

        public String adjustUseDepartment(String masterBillCode, String authorizationUseDepartmentId) {
            String sql = new SQL() {{
                UPDATE("b_master_bill_code t");
                SET("t.auth_use_department_id = #{authorizationUseDepartmentId}");
                WHERE("t.id=#{masterBillCode}");
            }}.toString();
            return sql;
        }

        public String delete(String masterBillCode) {
            String sql = new SQL() {{
                DELETE_FROM("b_master_bill_code t");
                WHERE("t.id = #{masterBillCode}");
            }}.toString();
            return sql;
        }

    }

    /**
     * `id` varchar(20) NOT NULL COMMENT '主单号ID,就是生成的主单号',
     * `airlines_id` varchar(20) DEFAULT NULL COMMENT '航空公司ID,关联xt_airlines.id',
     * `use_status_id` bigint(20) DEFAULT NULL COMMENT '使用状态ID,关联xt_dic.id key=''XT_DIC_USE_STATUS''',
     * `generate_department_id` varchar(20) DEFAULT NULL COMMENT '生成部门ID,关联xt_department.id',
     * `generate_date` date DEFAULT NULL COMMENT '生成日期',
     * `generate_user_id` varchar(20) DEFAULT NULL COMMENT '生成人ID,关联xt_user.id',
     * `auth_use_department_id` varchar(20) DEFAULT NULL COMMENT '授权使用部门ID,关联xt_department.id,默认是生成部门ID,可以由生成部门修改使用部门ID',
     * `use_department_id` varchar(20) DEFAULT NULL COMMENT '使用部门ID,关联xt_department.id',
     * `use_user_id` varchar(20) DEFAULT NULL COMMENT '使用人ID,关联xt_user.id',
     * `agent_company_id` varchar(20) DEFAULT NULL COMMENT '代理公司ID,关联xt_company.id'
     */


    @Results(id = "bMasterBillCodeMapId", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "airlines_id", property = "airlinesId"),
            @Result(column = "use_status_id", property = "useStatusId"),
            @Result(column = "generate_department_id", property = "generateDepartmentId"),
            @Result(column = "generate_date", property = "generateDate"),
            @Result(column = "generate_user_id", property = "generateUserId"),
            @Result(column = "auth_use_department_id", property = "authUseDepartmentId"),
            @Result(column = "use_department_id", property = "useDepartmentId"),
            @Result(column = "use_user_id", property = "useUserId"),
            @Result(column = "agent_company_id", property = "agentCompanyId")
    })
    @SelectProvider(type = BMasterBillCodeProvider.class, method = "findAll")
    List<BMasterBillCodePojo> findAll(Map map);


    @ResultMap("bMasterBillCodeMapId")
    @SelectProvider(type = BMasterBillCodeProvider.class, method = "fetch")
    BMasterBillCodePojo fetch(String id);

    /**
     * 主单号批量插入
     *
     * @param canSaveList
     * @return
     */
    @Insert(
            "<script>" +
                    "insert into b_master_bill_code(id, airlines_id ,use_status_id, generate_department_id, generate_date, generate_user_id, auth_use_department_id, agent_company_id) values " +
                    "<foreach collection='canSaveList' item='pojo' separator=','>" +
                    "(#{pojo.id},#{pojo.airlinesId},#{pojo.useStatusId},#{pojo.generateDepartmentId},#{pojo.generateDate},#{pojo.generateUserId},#{pojo.authUseDepartmentId},#{pojo.agentCompanyId})" +
                    "</foreach>" +
                    "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("canSaveList") List<BMasterBillCodePojo> canSaveList);


    @UpdateProvider(type = BMasterBillCodeProvider.class, method = "update")
    int update(BMasterBillCodePojo pojo);

    @DeleteProvider(type = BMasterBillCodeProvider.class, method = "delete")
    int delete(String masterBillCode);

    @UpdateProvider(type = BMasterBillCodeProvider.class, method = "adjustUseDepartment")
    int adjustUseDepartment(String masterBillCode, String authorizationUseDepartmentId);

}
